1 using UnityEngine;
2 using
System.Collections;
3
4 [RequireComponent (
typeof (Detonator))]
5 [AddComponentMenu(
"Detonator/Force")]
6 public
class DetonatorForce : DetonatorComponent
7 {
8     
private float _baseRadius = 50.0f;
9     
private float _basePower = 4000.0f;
10     
private float _scaledRange;
11     
private float _scaledIntensity;
12     
private bool _delayedExplosionStarted = false;
13     
private float _explodeDelay;
14     
15     
public float radius;
16     
public float power;
17     
public GameObject fireObject;
18     
public float fireObjectLife;
19     
20     
private Collider[] _colliders;
21     
private GameObject _tempFireObject;
22     
23     
override public void Init()
24     {
25         
//unused
26     }
27
28     
void Update()
29     {
30         
if (_delayedExplosionStarted)
31         {
32             _explodeDelay = (_explodeDelay - Time.deltaTime);
33             
if (_explodeDelay <= 0f)
34             {
35                 Explode();
36             }
37         }
38     }
39     
40     
private Vector3 _explosionPosition;
41     
42     
override public void Explode()
43     {
44         
if (!on) return;
45         
if (detailThreshold > detail) return;
46         
47         
if (!_delayedExplosionStarted)
48         {
49             _explodeDelay = explodeDelayMin + (Random.
value * (explodeDelayMax - explodeDelayMin));
50         }
51         
if (_explodeDelay <= 0) //if the delayTime is zero
52         {
53             
//tweak the position such that the explosion center is related to the explosion's direction
54             _explosionPosition = transform.position;
//- Vector3.Normalize(MyDetonator().direction);
55             _colliders = Physics.OverlapSphere (_explosionPosition, radius);
56
57             
foreach (Collider hit in _colliders)
58             {
59                 
if (!hit)
60                 {
61                     
continue;
62                 }
63                 
64                 
if (hit.GetComponent<Rigidbody>())
65                 {
66                     
//align the force along the object's rotation
67                     
//this is wrong - need to attenuate the velocity according to distance from the explosion center
68                     
//offsetting the explosion force position by the negative of the explosion's direction may help
69                     hit.GetComponent<Rigidbody>().AddExplosionForce((power * size), _explosionPosition, (radius * size), (
4f * MyDetonator().upwardsBias * size));
70                     
71                     
//fixed 6/15/2013 - didn't work before, was sending message to this script instead :)
72                     hit.gameObject.SendMessage(
"OnDetonatorForceHit", null, SendMessageOptions.DontRequireReceiver);
73                     
74                     
//and light them on fire for Rune
75                     
if (fireObject)
76                     {
77                         
//check to see if the object already is on fire. being on fire twice is silly
78                         
if (hit.transform.Find(fireObject.name+"(Clone)"))
79                         {
80                             
return;
81                         }
82                         
83                         _tempFireObject = (Instantiate(fireObject,
this.transform.position, this.transform.rotation)) as GameObject;
84                         _tempFireObject.transform.parent = hit.transform;
85                         _tempFireObject.transform.localPosition =
new Vector3(0f,0f,0f);
86                         
if (_tempFireObject.GetComponent<ParticleEmitter>())
87                         {
88                             _tempFireObject.GetComponent<ParticleEmitter>().emit =
true;
89                             Destroy(_tempFireObject,fireObjectLife);
90                         }
91                     }
92                 }
93             }
94             _delayedExplosionStarted =
false;
95             _explodeDelay =
0f;
96         }
97         
else
98         {
99             
//tell update to start reducing the start delay and call explode again when it's zero
100             _delayedExplosionStarted =
true;
101         }
102     }
103     
104     
public void Reset()
105     {
106         radius = _baseRadius;
107         power = _basePower;
108     }
109 }


Gõ tìm kiếm nhanh...